package com.sikaryofficial.backend.manager;

import cn.hutool.core.collection.CollUtil;
import com.sikaryofficial.backend.config.GuideTaskConfig;
import com.sikaryofficial.backend.constant.GuideTaskEnum;
import com.sikaryofficial.backend.domain.dto.req.points.GuideTaskReq;
import com.sikaryofficial.backend.domain.dto.resp.points.GuideTaskDTO;
import com.sikaryofficial.backend.domain.entity.GuideTaskDetail;
import com.sikaryofficial.backend.service.IGuideTaskDetailService;
import com.sikaryofficial.backend.service.guidetask.GuideTaskProcess;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : wuweihong
 * @desc : 指引任务管理类
 * @date : 2024-12-09
 */

@Component
@Slf4j
public class GuideTaskManager {

	@Autowired
	private IGuideTaskDetailService guideTaskDetailService;

	@Autowired
	private GuideTaskProcess guideTaskProcess;

	@Autowired
	private GuideTaskConfig guideTaskConfig;


	/**
	 * 执行指引任务
	 * @param req
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public Boolean processTask(GuideTaskReq req) {
		Long userId = SecurityUtils.getUserId();
		if (userId == 0) {
			throw new ServiceException("The user don't login.");
		}
		GuideTaskEnum guideTaskEnum = GuideTaskEnum.getByCode(req.getTaskCode());
		if (GuideTaskEnum.GO_TO_SKE != guideTaskEnum) {
			log.warn("You can not do the task({}).", req.getTaskCode());
			return false;
		}
		Integer points = guideTaskConfig.getPointsByTaskCode(req.getTaskCode());
		if (points.equals(0)) {
			throw new ServiceException("Unknown Task");
		}
		req.setUserId(userId);
		guideTaskProcess.processGuideTask(req);
		return true;
	}

	public List<GuideTaskDTO> getTaskList() {
		Long userId = SecurityUtils.getUserId();
		if (userId == 0) {
			throw new ServiceException("The user don't login.");
		}
		List<GuideTaskDetail> taskDetails = guideTaskDetailService.lambdaQuery()
				.eq(GuideTaskDetail::getDeletedVersion, 0)
				.eq(GuideTaskDetail::getCreatedBy, userId)
				.list();
		Map<String, GuideTaskDetail> taskDetailMap = new HashMap<>();
		if (CollUtil.isNotEmpty(taskDetails)) {
			taskDetailMap = taskDetails.stream().collect(Collectors.toMap(GuideTaskDetail::getTaskCode, item -> item));
		}
		List<GuideTaskDTO> taskDTOList = new ArrayList<>();
		for (GuideTaskEnum taskEnum : GuideTaskEnum.values()) {
			GuideTaskDTO taskDTO  = new GuideTaskDTO();
			taskDTO.setTaskCode(taskEnum.getTaskCode());
			taskDTO.setTaskDesc(taskDTO.getTaskDesc());
			taskDTO.setTaskNum(0);
			taskDTO.setDisabled(false);
			Integer points = guideTaskConfig.getPointsByTaskCode(taskEnum.getTaskCode());
			taskDTO.setPoints(points);
			if (taskDetailMap.containsKey(taskDTO.getTaskCode())) {
				taskDTO.setDisabled(true);
				taskDTO.setTaskNum(taskDTO.getTaskNum());
			}
			taskDTOList.add(taskDTO);
		}
		return taskDTOList;
	}

}
